package com.irdstudio.bsp.console.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.irdstudio.bsp.console.dao.PluginNlsJobConfDao;
import com.irdstudio.bsp.console.dao.domain.PluginNlsJobConf;
import com.irdstudio.bsp.console.service.facade.PluginNlsJobConfService;
import com.irdstudio.bsp.console.service.vo.PluginNlsJobConfVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.sdk.beans.core.base.FrameworkService;

/**
 * <p>
 * JAVA网贷系统作业插件配置的Service实现类
 * </p>
 * @author szbd
 * @createDate 2020-06-22 14:49:40
 */
@Service("nlsJobConfService")
public class PluginNlsJobConfServiceImpl implements PluginNlsJobConfService, FrameworkService {
    
    private static Logger logger = LoggerFactory.getLogger(PluginNlsJobConfServiceImpl.class);
    
    @Autowired
    private PluginNlsJobConfDao pluginNlsJobConfDao;
    
    @Override
    public int insertPluginNlsJobConf(PluginNlsJobConfVO inPluginJobPreimplConfVo) {
        logger.info("新增JAVA网贷系统作业插件配置数据的参数为：" + JSONObject.toJSONString(inPluginJobPreimplConfVo));
        if(Objects.isNull(inPluginJobPreimplConfVo)){
            return 0;
        }
        int count = 0;
        try {
            PluginNlsJobConf pluginJobPreimplConf = new PluginNlsJobConf();
            beanCopy(inPluginJobPreimplConfVo, pluginJobPreimplConf);
            count = pluginNlsJobConfDao.insertPluginNlsJobConf(pluginJobPreimplConf);
        } catch (Exception e) {
            logger.error("新增JAVA网贷系统作业插件配置数据发生异常！", e);
            count = -1;
        }
        logger.info("当前新增JAVA网贷系统作业插件配置数据条数为：" + count);
        return count;
    }
    
    @Override
    public int deletePluginNlsJobConfByPk(PluginNlsJobConfVO inPluginJobPreimplConfVo) {
        logger.info("删除JAVA网贷系统作业插件配置数据的参数为：" + JSONObject.toJSONString(inPluginJobPreimplConfVo));
        if(Objects.isNull(inPluginJobPreimplConfVo)){
            return 0;
        }
        int count = 0;
        try {
            PluginNlsJobConf pluginJobPreimplConf = new PluginNlsJobConf();
            beanCopy(inPluginJobPreimplConfVo, pluginJobPreimplConf);
            count = pluginNlsJobConfDao.deletePluginNlsJobConfByPk(pluginJobPreimplConf);
        } catch (Exception e) {
            logger.error("删除JAVA网贷系统作业插件配置数据发生异常！", e);
            count = -1;
        }
        logger.info("删除的JAVA网贷系统作业插件配置数据条数为：" + count);
        return count;
    }
    
    @Override
    public int updatePluginNlsJobConfByPk(PluginNlsJobConfVO inPluginJobPreimplConfVo) {
        logger.info("修改JAVA网贷系统作业插件配置数据的参数为：" + JSONObject.toJSONString(inPluginJobPreimplConfVo));
        if(Objects.isNull(inPluginJobPreimplConfVo)){
            return 0;
        }
        int count = 0;
        try {
            PluginNlsJobConf pluginJobPreimplConf = new PluginNlsJobConf();
            beanCopy(inPluginJobPreimplConfVo, pluginJobPreimplConf);
            count = pluginNlsJobConfDao.updatePluginNlsJobConfByPk(pluginJobPreimplConf);
        } catch (Exception e) {
            logger.error("修改数据发生异常！", e);
            count = -1;
        }
        logger.info("修改的JAVA网贷系统作业插件配置数据条数为" + count);
        return count;
    }
    
    @Override
    public PluginNlsJobConfVO queryPluginNlsJobConfByPk(PluginNlsJobConfVO inPluginJobPreimplConfVo) {
        logger.info("根据主键查询JAVA网贷系统作业插件配置的请求参数为：" + JSONObject.toJSONString(inPluginJobPreimplConfVo));
        if(Objects.isNull(inPluginJobPreimplConfVo)){
            return null;
        }
        try {
            PluginNlsJobConf pluginJobPreimplConf = new PluginNlsJobConf();
            beanCopy(inPluginJobPreimplConfVo, pluginJobPreimplConf);
            pluginJobPreimplConf = pluginNlsJobConfDao.queryPluginNlsJobConfByPk(pluginJobPreimplConf);
            if (Objects.nonNull(pluginJobPreimplConf)) {
                PluginNlsJobConfVO outPluginJobPreimplConfVo = beanCopy(pluginJobPreimplConf, new PluginNlsJobConfVO());
                return outPluginJobPreimplConfVo;
            } else {
                logger.info("根据主键查询JAVA网贷系统作业插件配置的记录为空！");
            }
        } catch (Exception e) {
            logger.error("根据主键查询JAVA网贷系统作业插件配置记录发生异常！", e);
        }
        return null;
    }
    
    @Override
    public PluginNlsJobConfVO querySinglePluginNlsJobConfByCondition(PluginNlsJobConfVO inPluginJobPreimplConfVo) {
        logger.info("根据条件查询单笔JAVA网贷系统作业插件配置的请求参数为：" + JSONObject.toJSONString(inPluginJobPreimplConfVo));
        if(Objects.isNull(inPluginJobPreimplConfVo)){
            return null;
        }
        try {
            PluginNlsJobConf pluginJobPreimplConf = new PluginNlsJobConf();
            beanCopy(inPluginJobPreimplConfVo, pluginJobPreimplConf);
            pluginJobPreimplConf = pluginNlsJobConfDao.querySinglePluginNlsJobConfByCondition(pluginJobPreimplConf);
            if (Objects.nonNull(pluginJobPreimplConf)) {
                PluginNlsJobConfVO outPluginJobPreimplConfVo = beanCopy(pluginJobPreimplConf, new PluginNlsJobConfVO());
                return outPluginJobPreimplConfVo;
            } else {
                logger.info("根据条件查询单笔JAVA网贷系统作业插件配置的记录为空！");
            }
        } catch (Exception e) {
            logger.error("根据条件查询单笔JAVA网贷系统作业插件配置记录发生异常！", e);
        }
        return null;
    }
    
    @Override
    public List<PluginNlsJobConfVO> queryPluginNlsJobConfByCondition(PluginNlsJobConfVO inPluginJobPreimplConfVo) {
        logger.info("条件查询JAVA网贷系统作业插件配置的请求参数为：" + JSONObject.toJSONString(inPluginJobPreimplConfVo));
        if(Objects.isNull(inPluginJobPreimplConfVo)){
            return null;
        }
        try {
            List<PluginNlsJobConf> dataList = pluginNlsJobConfDao.queryPluginNlsJobConfByCondition(inPluginJobPreimplConfVo);
            if (Objects.nonNull(dataList)) {
                List<PluginNlsJobConfVO> resultList = new ArrayList<PluginNlsJobConfVO>();
                resultList = (List<PluginNlsJobConfVO>) beansCopy(dataList, PluginNlsJobConfVO.class);
                return resultList;
            } else {
                logger.info("条件查询JAVA网贷系统作业插件配置的数据为空！");
            }
        } catch (Exception e) {
            logger.error("条件查询JAVA网贷系统作业插件配置发生异常！", e);
        }
        return null;
    }
    
    @Override
    public List<PluginNlsJobConfVO> queryPluginNlsJobConfByConditionByPage(PluginNlsJobConfVO inPluginJobPreimplConfVo) {
        logger.info("条件分页查询JAVA网贷系统作业插件配置的请求参数为：" + JSONObject.toJSONString(inPluginJobPreimplConfVo));
        if(Objects.isNull(inPluginJobPreimplConfVo)){
            return null;
        }
        try {
            List<PluginNlsJobConf> dataList = pluginNlsJobConfDao.queryPluginNlsJobConfByConditionByPage(inPluginJobPreimplConfVo);
            if (Objects.nonNull(dataList)) {
                List<PluginNlsJobConfVO> resultList = new ArrayList<PluginNlsJobConfVO>();
                pageSet(resultList, inPluginJobPreimplConfVo);
                resultList = (List<PluginNlsJobConfVO>) beansCopy(dataList, PluginNlsJobConfVO.class);
                return resultList;
            } else {
                logger.info("条件分页查询JAVA网贷系统作业插件配置的数据为空！");
            }
        } catch (Exception e) {
            logger.error("条件分页查询JAVA网贷系统作业插件配置发生异常！", e);
        }
        return null;
    }

    @Override
    public List<PluginNlsJobConfVO> queryPluginNlsJobConf4Select(Integer pluginId, String taskId) {
        List<PluginNlsJobConfVO> resultList = null;
        try {
            List<PluginNlsJobConf> dataList = pluginNlsJobConfDao.queryPluginNlsJobConf4Select(pluginId, taskId);
            if (Objects.nonNull(dataList)) {
                resultList = new ArrayList<>();
                resultList = (List<PluginNlsJobConfVO>) beansCopy(dataList, PluginNlsJobConfVO.class);
            }
        } catch (Exception e) {
            logger.error("查询JAVA网贷系统作业插件配置发生异常！", e);
        }
        return resultList;
    }
}